package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.JdbcTemplate.WXJdbc;
import com.wuseguang.springcloud.dao.jdbc.TeacherSubscriptionJdbc;
import com.wuseguang.springcloud.dao.repository.TeacherSubscriptionRepository;
import com.wuseguang.springcloud.entity.TeacherSubscription;
import com.wuseguang.springcloud.quartz.JobOperation;
import com.wuseguang.springcloud.service.TeacherSubscriptionService;
import com.wuseguang.springcloud.util.CronUtils;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 教师订阅表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-05-26
 */
@Service
public class TeacherSubscriptionServiceImpl implements TeacherSubscriptionService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TeacherSubscriptionJdbc teacherSubscriptionJdbc;
    @Autowired
    private   JobOperation jobOperation;
    @Autowired
    private TeacherSubscriptionRepository teacherSubscriptionRepository;

    @Override
    public Result findTeacherSubscription(String schoolId, String teacherId) {
        logger.info("查询教师是否订阅了课表推送");
        String sql = "select t.`name` teacherName,if(ts.id is null ,0,ts.state) state" +
                " ,t.id teacherId  from teacher_subscription ts right JOIN teacher t" +
                " on(ts.teacher_id=t.id and ts. type = 'timetable') where t.id =? ";
        //查询教师课表推送订阅信息
        Map<String,Object> map = teacherSubscriptionJdbc.queryMap(sql,teacherId);

        return new Result(map);
    }

    @Override
    public Result scheduleSubscription(Integer state, String teacherId, String schoolId) {

        //修改订阅信息

        TeacherSubscription t =  teacherSubscriptionRepository.findByTeacherId(teacherId);
        //判断如果为空
        if(StringUtils.isEmpty(t)||t==null){
            t = new TeacherSubscription();
            t.setSchoolId(Integer.valueOf(schoolId));
            t.setState(state);
            t.setType("timetable");
            t.setTeacherId(Integer.valueOf(teacherId));
        }else{
            t.setState(state);
        }

        teacherSubscriptionRepository.save(t);
        return new Result();
    }

    @Override
    public Result openTask(String schoolId) {

        //查询学校的上课节次跟上课次数
        String sql = "select start_time,current_section from attend_class_time where school_id =? ORDER BY current_section asc";
        //上课的节次时间
        List<Map<String,Object>> list = teacherSubscriptionJdbc.queryListMap(sql,schoolId);
        //要触发的时间
        List<String> cronList = new ArrayList<>();
        //循环把时间转成corn
        for (Map<String,Object> map :list) {
            //cron表达式
            String cron = CronUtils.formatDateByPattern2(map.get("start_time").toString());
            cronList.add(cron);
        }

        try {
            //添加前先把这个以前的删除了
            jobOperation.remove("timetableTimer",schoolId);
            jobOperation.add("timetableTimer",schoolId,cronList);
        } catch (Exception e) {
            logger.error("教师推送创建失败",e);
        }
        return new Result();
    }


}
